package org.hxzon.workeasy.pages.contacts;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.workeasy.model.EasyContacts;
import org.hxzon.workeasy.pages.base.MyBasePage;
import org.hxzon.workeasy.service.EasyContactsService;

public class EditContacts extends MyBasePage {
    @Inject
    private EasyContactsService contactsSer;
    @InjectPage
    private ListContacts listContacts;
    @Property
    private EasyContacts contacts;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String id) {
        checkUserIsAdmin();
        //init for display or update
        contacts = contactsSer.findById(id);
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return contacts.getId();
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "update")
    Object update() {
        contactsSer.save(contacts);
        return listContacts;
    }

}
